
/* GCSx
** SCENEPROP.CPP
**
** Scene properties dialog
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

// Properties dialog

ScenePropertiesDialog* ScenePropertiesDialog::dialog = NULL;

ScenePropertiesDialog* ScenePropertiesDialog::create() { start_func
    if (!dialog) {
        dialog = new ScenePropertiesDialog();
    }
    return dialog;
}

void ScenePropertiesDialog::destroy() { start_func
    if (dialog) {
        delete dialog;
        dialog = NULL;
    }
}

ScenePropertiesDialog::ScenePropertiesDialog() : Dialog("Scene Properties") { start_func
    initialized = 0;
}

Dialog::ButtonAction ScenePropertiesDialog::verifyEntry(int buttonId, Dialog::ButtonAction buttonType) { start_func
    if ((buttonType != BUTTON_APPLY) && (buttonType != BUTTON_OK)) return BUTTON_DEFAULT;

    // Another scene with same name?
    string lName = dynamic_cast<WTextBox*>(findWidget(ID_NAME))->state();
    toLower(lName);
    const Scene* found = scene->getWorld()->findScene(lName);
    if ((found) && (found != scene)) {
        guiErrorBox("A scene by that name already exists- please choose another.", errorTitleDuplicateName);
        return BUTTON_NOTHING;
    }
    
    if (lName.empty()) {
        guiErrorBox("Please enter a name for this scene.", errorTitleMissingName);
        return BUTTON_NOTHING;
    }

    return Dialog::verifyEntry(buttonId, buttonType);
}

int ScenePropertiesDialog::run(string* title, const Scene* whichScene) { start_func
    scene = whichScene;
    
    if (!initialized) {
        Widget* w = NULL;
    
        w = new WStatic(ID_LABEL, "\tName:");
        w->addTo(this);

        w = new WTextBox(ID_NAME, title, 0);
        w->addTo(this);

        w = new WButton(ID_OK, messageBoxOK, BUTTON_OK);
        w->addTo(this);
        
        w = new WButton(ID_CANCEL, messageBoxCancel, BUTTON_CANCEL);
        w->addTo(this);
        
        makePretty();
        
        initialized = 1;
    }
    else {
        findWidget(ID_NAME)->changeStorage(title);
    }
    
    // Hit OK?
    if (runModal() == ID_OK) return 1;
    return 0;
}

